package com.wswy.wzcx.ui.adapter;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.listener.RequestLoggingListener;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.UserMode;
import com.wswy.wzcx.ui.hold.ViewHolder;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.data.UserMessage;

import java.util.List;


/**
 * Created by wangduheng26 on 2018/1/22.
 * wangduheng26@gmail.com
 */

public class UserMessageAdapter extends BaseAdapter<UserMessage, UserMessageAdapter.UserMessageVH> {
    private static final int TYPE_SYSTEM = 1;
    private static final int TYPE_USER = 2;
    private UserMode userMode;

    public UserMessageAdapter(List<UserMessage> list) {
        super(list);
        if (DataCenter.get().hasLogin()) {
            userMode = DataCenter.get().getUserMode();
        }
    }

    @Override
    public UserMessageVH onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        if (viewType == TYPE_SYSTEM) {
            return new UserMessageVH(inflater.inflate(R.layout.item_message_system, parent, false));
        } else {
            return new UserMessageVH(inflater.inflate(R.layout.item_message_user, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        final UserMessage item = getItem(position);
        if (item != null) {
            if(item.isAdmin){
                return TYPE_SYSTEM;
            }else {
                return TYPE_USER;
            }
        }
        return super.getItemViewType(position);
    }


    @Override
    public void onBindViewHolder(UserMessageVH holder, UserMessage item, int position) {
        holder.bind(item);
        int itemViewType = getItemViewType(position);
        if (itemViewType == TYPE_USER && userMode != null) {
            Uri uri = UriUtil.parseUriOrNull(userMode.getHeadImg());
            ImageRequest builder = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(ResizeOptions.forSquareSize(AndroidUtilities.dp(100)))
                    .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
                    .setRequestListener(new RequestLoggingListener())
                    .build();
            AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.ivHeader.getController())
                    .setImageRequest(builder)
                    .build();
            holder.ivHeader.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);
            holder.ivHeader.setController(controller);
        }

    }

    public class UserMessageVH extends ViewHolder {
        SimpleDraweeView ivHeader;
        TextView tvContent;
        TextView tvTime;

        public UserMessageVH(View itemView) {
            super(itemView);

            ivHeader = itemView.findViewById(R.id.image_view_header);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);

//            FrescoUtils.load();
//            ivHeader.setImageURI();
        }

        public void bind(UserMessage item) {
            if (item != null) {
                tvContent.setText(item.content);
                tvTime.setText(TimeUtils.millis2String(item.createTime*1000));

            }
        }
    }
}
